﻿<script>
    function addClass() {
        disablesave();
        $("#DBClassName").val("");
        $("#DBClassCode").val("");
        $("#DBClassCode").removeAttr("disabled", "disabled");
        $(".talkDiv-title span").html("添加分类");
        $(".talkDivBg").show();
        $(".form-h-8em").show();
    }
    function closeClass() {
        $(".talkDivBg").hide();
        $(".form-h-8em").hide();
        enablesave();
    }
    function editClass() {
        disablesave();
        var len = $('.f-ct input:checked').length;
        if (len == 1) {
            var value = $('.f-ct input:checked').val();
            var code = $.trim(value);
            var name = $.trim($('.f-ct input:checked').parent().text());
            $("#DBClassName").val(name);
            $("#DBClassCode").val(code);
            $("#DBClassCode").attr("disabled", "disabled");
            
            $(".talkDiv-title span").html("编辑分类");
            $(".talkDivBg").show();
            $(".form-h-8em").show();
        }
        else {
            messageAlert("请选择需要编辑的数据库分类（只能选择一个）");
            enablesave();
            return;

        }
    }
    function delClass() {
        var len = $('.f-ct input:checked').length;
        if (len > 0) {
            confirmDialog('确定要删除吗?', function () {
                    var checked = [];
                    $('.f-ct input:checked').each(function () {
                        checked.push($(this).val());
                    })
                    var urlstr = "@Html.Raw(Url.Action("DBClassDel", "Admin_MultiDBPublish"))";
                    var dataobj = { "dbcode": checked.toString() };
                    ajaxinfo(urlstr, dataobj);
                })
        }
        else {
            messageAlert("请选择要删除的数据库分类");
            enablesave();
            return;
        }
    }

    //调用增删改查方法
    function ajaxinfo(urlaction, datainfo) {
        $.ajax({
            type: "GET",
            url: timestamp(urlaction),
            data: datainfo,
            success: function (msg) {
                if (msg == "True") {
                    messageAlert("操作成功！");
                    enablesave();
                    var urlstr = "@Html.Raw(Url.Action("NewClassify", "Admin_MultiDBPublish"))";
                    ajaxLoad("initload", urlstr);//刷新页面
                }
                else {
                    messageAlert("操作失败！");
                    enablesave();
                }
            },
            error: function (e)
            { enablesave(); }
        })
        enablesave();
    }
    function saveClass() {
        disablesave();
        var dbclassname = $.trim($("#DBClassName").val());
        var dbclasscode = $.trim($("#DBClassCode").val());
        if (dbclassname == "" || dbclassname == " ") {
            messageAlert("请输入分类名称！");
            enablesave();
            return;
        }
        else {

            if (!regtext(dbclassname)) {
                messageAlert("请输入分类名称(不允许输入特殊字符)！");
                enablesave();
                return;
            }
        }
        if (dbclasscode == "") {
            messageAlert("请输入分类代码！");
            enablesave();
            return;
        }
        else {
            if (!regtext(dbclasscode)) {
                messageAlert("请输入分类代码(不允许输入特殊字符)！");
                enablesave();
                return;
            }
        }
        //判读是否和已存在的分类重名
        var m = 0;
        var n = 0;
        var flage = "save";
        if ($("#DBClassCode").attr("disabled") == "disabled") //修改
        {
            $('.f-ct input[type=checkbox]').each(function () {
                if ($(this).attr("checked") != "checked" && $.trim($(this).parent().text()) == dbclassname) {
                    n = 1;
                }
            })
            if (n == 1) {
                messageAlert("存在重复的分类名称，请修改！");
                return;
            }
            flage = "update";
        }
        else {  //新增
            $('.f-ct input[type=checkbox]').each(function () {
                if ($.trim($(this).val()) == dbclasscode) {
                    m = 1;
                }
                if ($.trim($(this).parent().text()) == dbclassname) {
                    n = 1;
                }
            })
            if (m == 1) {
                messageAlert("存在重复的分类代码，请修改！");
                return;
            }
            if (n == 1) {
                messageAlert("存在重复的分类名称，请修改！");
                return;
            }
        }
        var urlstr = "@Html.Raw(Url.Action("DBClassSave", "Admin_MultiDBPublish"))";
        var dataobj = { "name": dbclassname, "code": dbclasscode, "isSave": flage };
        ajaxinfo(urlstr, dataobj);
        closeClass();
    }
    function addDB() {
        disablesave();
        var len = $('.f-ct input:checked').length;
        if (len == 1) {
            var code = $('.f-ct input:checked').val();
            code = $.trim(code);
            var urlstr = "@Html.Raw(Url.Action("DBClassify", "Admin_MultiDBPublish"))";
            var dataobj = { "code": code};
            ajaxLoad("initload", urlstr, dataobj);//刷新页面
        }
        else {
            messageAlert("请选择需要编辑的数据库分类（只能选择一个）");
            enablesave();
        }
    }
    function disablesave() {
        $(".btn-defa-f").attr("disabled", "disabled");
    }
    function enablesave() {
        $(".btn-defa-f").removeAttr("disabled", "disabled");
    }
</script>
<div class="wrapR-h form-h form-h-10em" style="padding-top:20px">
    <div>
        <button class="btn btn-m btn-submit btn-defa-f"  onclick="addClass()">添加分类</button>
        <button class="btn btn-m btn-submit btn-defa-f"  onclick="editClass()">编辑分类</button>
        <button class="btn btn-m btn-submit btn-defa-f"  onclick="delClass()">删除分类</button>
        <button class="btn btn-m btn-submit btn-defa-f"  onclick="addDB()">添加可跨库的单库</button>
    </div>
</div>
<div class="wrapR-c">
    <ul>
        @if (ViewBag.ClassDB != null && ViewBag.ClassDB.Count > 0)
        {
            foreach (CNKI.TPI.Web.Search.Model.DBCLASS item in ViewBag.ClassDB) //分类
            {
                
            <li class='has-classfiy-li'>
                <div class="classfiy-con-title">
                    <div class="f-ct">
                        <label class="f-label" style="color: #107BCE;">
                            <input type="checkbox" class="f-checkbox" value="@item.ClassCode">@item.ClassName</label>
                    </div>
                </div>
                <div class="classfiy-con-b">
                    <div class="f-ct" >
                        <ul class='clearfix'>
                            @if (ViewBag.DBConfig != null && ViewBag.DBConfig.Count > 0)
                            {
                                foreach (CNKI.TPI.Web.Search.Model.DBCONFIG itemconfig in ViewBag.DBConfig)
                                {
                                    if (itemconfig.ClassID.Contains(item.ClassCode))// 分类下的数据库
                                    {
                                        <li style="text-decoration: none; cursor: default">
                                            <label class="f-label" style="text-decoration: none; cursor: default">@itemconfig.CNName</label>
                                        </li>
                                    }
                                }
                            }

                        </ul>

                    </div>
                </div>
            </li>
            }
        }

    </ul>

</div>

  <div class="talkDivBg" style="display: none;">
    <!--talkDiv弹出层(不带图片，宽550)-->
    <div class="talkDiv talkW-550 form-h form-h-8em" style="display: none">
        <div class="talkDiv-title">
            <span>添加分类</span>
            <a class="icon icon-close" title="关闭" href="javascript:void(0);" onclick="closeClass()"><i class="iconfont icon-error3"></i></a>
        </div>
        <div class="talkDiv-body">

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label" style="text-decoration: none; cursor: default">数据库分类名称：</label>
                </div>
                <div class="f-ct">
                    <input type="text" value="Text" class="f-text span4" id="DBClassName" maxlength="16">  <span class="f-label-re">*</span>
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label" style="text-decoration: none; cursor: default">数据库分类代码：</label>
                </div>
                <div class="f-ct">
                    <input type="text" value="Text" class="f-text span4" id="DBClassCode" maxlength="16" onkeyup="value=value.replace(/[W]/g,'')" />  <span class="f-label-re">*</span>
                </div>
            </div>

        </div>
        <div class="talkDiv-btn"><a href="javascript:void(0);" class="btn btn-m btn-prim-f btn-submit" onclick="saveClass()">保存</a> </div>
    </div>
</div>